import { ElMessage } from 'element-plus';
import axios from 'axios'

// 默认失败信息 - 包装成统一的信息
const defaultFailure = (message, code, url) => {
    console.warn(`请求地址: ${url}, 状态码: ${code}, 错误信息: ${message}`)
    ElMessage.warning(message)
}

// 默认错误信息 - 直接控制台输出
const defaultError = (err) => {
    console.error(err)
    ElMessage.warning('发生未知错误，请联系管理员')
}

function internalPut(url, data, header, success, failure, error = defaultError){
    axios.put(url, data, {headers: header}).then(({ data }) => {
        if(data.code === 200){
            success(data.data)
        } else {
            failure(data.message, data.code, url)
        }
    }).catch(err => error(err))
}

function reset(id, success, failure = defaultFailure){
    internalPut('/api/auth/reset-password', {
        id: id
    }, {
        'Content-Type': 'multipart/form-data'
    },() => {
        ElMessage.success('重置密码成功')
    }, failure)
}

export {
    reset
}